package com.yuju.login.service.impl;

import java.io.UnsupportedEncodingException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;

import org.apache.commons.codec.binary.Hex;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.yuju.cache.RedisCache;
import com.yuju.common.core.dao.IBaseDao;
import com.yuju.common.core.service.AbstractBaseService;
import com.yuju.constant.CacheKey;
import com.yuju.enums.LoginReturnCodeEnum;
import com.yuju.enums.RegisterReturnCodeEnum;
import com.yuju.exception.BusinessException;
import com.yuju.login.dao.entity.Op;
import com.yuju.login.model.OpModel;
import com.yuju.login.service.ILoginService;
import com.yuju.login.service.IOpService;
import com.yuju.util.Utils;

/**
 * 
 * 类名：LoginServiceImpl  <br />
 *
 * 功能：微信登录
 *
 * @author : zhousc <br />
 * @Date : 2018年1月8日 上午10:22:24  <br />
 * @version : 1.1 <br />
 */
@Service
public class LoginServiceImpl extends AbstractBaseService<OpModel, Op, String> implements ILoginService{

	@Autowired
	private IOpService opService;
	
	public IBaseDao<OpModel, Op, String> getDao() {
		// TODO Auto-generated method stub
		return null;
	}
	


	/**
	 * 
	 * 功能:密码登录 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2018年1月8日 上午10:22:35<br/>
	 * 
	 * @param convert
	 * @return <br/>
	 * @see com.yuju.login.service.ILoginService#wxLogin(com.yuju.login.model.OpModel)
	 */
	@Override
	public OpModel quietLogin(String id) {
		OpModel paramModel = new OpModel();
		paramModel.setId(id);
		List<OpModel> list = opService.list(paramModel);
		//返回未注册
		if(list.size() == 0){
			throw new BusinessException(LoginReturnCodeEnum.NOT_REGISTERED.getCode()
					, LoginReturnCodeEnum.NOT_REGISTERED.getDesc());
		}
		
		return list.get(0);
	}

	/**
	 * 
	 * 功能:密码登录 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2018年1月8日 上午10:22:35<br/>
	 * 
	 * @param convert
	 * @return <br/>
	 * @see com.yuju.login.service.ILoginService#wxLogin(com.yuju.login.model.OpModel)
	 */
	@Override
	public OpModel wxLogin(OpModel opModel) {
		//验证手机号是否注册
		OpModel selectByMobile = new OpModel();
		selectByMobile.setName(opModel.getMobile());
		List<OpModel> list = opService.list(selectByMobile);
		//返回未注册
		if(list.size() == 0 ){
			throw new BusinessException(LoginReturnCodeEnum.NOT_REGISTERED.getCode()
					, LoginReturnCodeEnum.NOT_REGISTERED.getDesc());
		}
		//验证密码
		OpModel model = list.get(0);
		String pwd = model.getUserId() + opModel.getPassword() + model.getSalt();
		//进行SHA加密
		try {
			MessageDigest instance = MessageDigest.getInstance("SHA-256");
			byte[] digest = instance.digest(pwd.getBytes("UTF-8"));
			//产生加密密码
			String encodeHexString = Hex.encodeHexString(digest);
			if(encodeHexString.equals(model.getPassword())){
				//验证成功返回账号信息
				return model;
			}else {
				throw new BusinessException(LoginReturnCodeEnum.LOGIN_FAILURE.getCode()
						, LoginReturnCodeEnum.LOGIN_FAILURE.getDesc());
			}
		} catch (NoSuchAlgorithmException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (UnsupportedEncodingException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	/**
	 * 
	 * 功能:验证码登录 <br/>
	 * 
	 * 重写：zhousc <br/>
	 * 
	 * @Date ：2018年1月8日 上午10:56:14<br/>
	 * 
	 * @param convert
	 * @return <br/>
	 * @see com.yuju.login.service.ILoginService#verificationCodeLogin(com.yuju.login.model.OpModel)
	 */
	@Override
	public OpModel verificationCodeLogin(OpModel opModel) {
		//根据手机号验证验证码
		RedisCache simpleCache = RedisCache.getInstance();
		String cacheVerCode = (String) simpleCache.get(opModel.getMobile() + CacheKey.KEY_CACHE_LOGIN_VERIFY_CODE);
		//如果验证码失效，抛出失效异常信息
		if(Utils.isNull(simpleCache)){
			throw new BusinessException(RegisterReturnCodeEnum.CODE_FAILURE.getCode()
					, RegisterReturnCodeEnum.CODE_FAILURE.getDesc());
		}
		//判断验证码是否正确，错误则抛出 ，验证码错误异常
		if(!opModel.getVerificationCode().equals(cacheVerCode) ){
			throw new BusinessException(RegisterReturnCodeEnum.CODE_VAILD_FAILURE.getCode()
					, RegisterReturnCodeEnum.CODE_VAILD_FAILURE.getDesc());
		}
		//验证成功，获取账号信息 返回
		OpModel selectByMobile = new OpModel();
		selectByMobile.setMobile(opModel.getMobile());
		List<OpModel> list = opService.list(selectByMobile);
		return list.get(0);
	}
}